|
1
|
|
package org.marketchangers.pico;
|
|
2
|
|
|
|
3
|
|
import java.io.IOException;
|
|
4
|
|
import java.io.InputStream;
|
|
5
|
|
import java.util.Properties;
|
|
6
|
|
|
|
7
|
|
import javax.naming.InitialContext;
|
|
8
|
|
import javax.naming.NamingException;
|
|
9
|
|
import javax.servlet.ServletContext;
|
|
10
|
|
|
|
11
|
|
import net.sf.hibernate.HibernateException;
|
|
12
|
|
import net.sf.hibernate.MappingException;
|
|
13
|
|
import net.sf.hibernate.cfg.Configuration;
|
|
14
|
|
import net.sf.hibernate.cfg.Environment;
|
|
15
|
|
import net.sf.hibernate.dialect.HSQLDialect;
|
|
16
|
|
|
|
17
|
|
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
|
18
|
|
import org.apache.lucene.store.RAMDirectory;
|
|
19
|
|
import org.marketchangers.ServiceLocator;
|
|
20
|
|
import org.marketchangers.defaults.SimpleEnvironment;
|
|
21
|
|
import org.marketchangers.liferay.LiferayUserManager;
|
|
22
|
|
import org.marketchangers.prayer.PrayerCategory;
|
|
23
|
|
import org.marketchangers.prayer.PrayerComment;
|
|
24
|
|
import org.marketchangers.prayer.PrayerRequest;
|
|
25
|
|
import org.marketchangers.prayer.PrayerRequestSearcher;
|
|
26
|
|
import org.marketchangers.prayer.UserContract;
|
|
27
|
|
import org.marketchangers.prayer.hibernate.HibernatePrayerRequestManager;
|
|
28
|
|
import org.marketchangers.prayer.lucene.DefaultIndexProvider;
|
|
29
|
|
import org.marketchangers.prayer.lucene.LucenePrayerRequestSearcher;
|
|
30
|
|
import org.nanocontainer.concurrent.ThreadLocalComponentAdapter;
|
|
31
|
|
import org.nanocontainer.ejb.EJBClientComponentAdapter;
|
|
32
|
|
import org.nanocontainer.hibernate.LifecycleSessionProvider;
|
|
33
|
|
import org.nanocontainer.hibernate.SessionFactoryComponentAdapter;
|
|
34
|
|
import org.nanocontainer.hibernate.SessionProvider;
|
|
35
|
|
import org.nanocontainer.integrationkit.ContainerComposer;
|
|
36
|
|
import org.picocontainer.MutablePicoContainer;
|
|
37
|
|
import org.picocontainer.PicoContainer;
|
|
38
|
|
import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
|
|
39
|
|
|
|
40
|
|
import com.liferay.portal.ejb.RoleManagerHome;
|
|
41
|
|
import com.liferay.portal.ejb.RoleManagerHomeUtil;
|
|
42
|
|
import com.liferay.portal.ejb.UserRemoteManagerHome;
|
|
43
|
|
import com.liferay.portal.ejb.UserRemoteManagerHomeUtil;
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
public class PicoContainerComposer implements ContainerComposer {
|
|
49
|
|
private InitialContext jndiContext;
|
|
50
|
|
|
|
51
|
0
|
public PicoContainerComposer() throws HibernateException, NamingException {
|
|
52
|
0
|
jndiContext = new InitialContext();
|
|
53
|
|
}
|
|
54
|
|
|
|
55
|
|
|
|
56
|
0
|
public void composeContainer(MutablePicoContainer pico, Object scope) {
|
|
57
|
0
|
if (scope instanceof ServletContext) {
|
|
58
|
|
|
|
59
|
0
|
ServletContext context = (ServletContext) scope;
|
|
60
|
0
|
context.setAttribute(ServiceLocator.KEY, new PicoServiceLocator(pico));
|
|
61
|
|
|
|
62
|
|
|
|
63
|
0
|
pico.registerComponentImplementation(SimpleEnvironment.class);
|
|
64
|
|
|
|
65
|
|
|
|
66
|
0
|
pico.registerComponentImplementation(LiferayUserManager.class);
|
|
67
|
0
|
pico.registerComponent(new EJBClientComponentAdapter(UserRemoteManagerHomeUtil.NAME, UserRemoteManagerHome.class, jndiContext));
|
|
68
|
0
|
pico.registerComponent(new EJBClientComponentAdapter(RoleManagerHomeUtil.NAME, RoleManagerHome.class, jndiContext));
|
|
69
|
|
|
|
70
|
|
|
|
71
|
0
|
pico.registerComponentImplementation(HibernatePrayerRequestManager.class);
|
|
72
|
0
|
pico.registerComponent(new ThreadLocalComponentAdapter(new ConstructorInjectionComponentAdapter(LifecycleSessionProvider.class, LifecycleSessionProvider.class)));
|
|
73
|
0
|
pico.registerComponent(new SessionFactoryComponentAdapter());
|
|
74
|
0
|
pico.registerComponentInstance(getHibernateConfiguration(Thread.currentThread().getContextClassLoader().getResourceAsStream("marketchangers-hibernate.properties")));
|
|
75
|
|
|
|
76
|
|
|
|
77
|
0
|
pico.registerComponentImplementation(LucenePrayerRequestSearcher.class);
|
|
78
|
0
|
pico.registerComponentImplementation(DefaultIndexProvider.class);
|
|
79
|
|
|
|
80
|
0
|
pico.registerComponentImplementation(StandardAnalyzer.class);
|
|
81
|
0
|
pico.registerComponentImplementation(RAMDirectory.class);
|
|
82
|
0
|
indexPrayerRequests(pico);
|
|
83
|
|
}
|
|
84
|
|
}
|
|
85
|
|
|
|
86
|
|
|
|
87
|
0
|
private Configuration getHibernateConfiguration(InputStream values) {
|
|
88
|
0
|
Properties p = new Properties();
|
|
89
|
0
|
p.setProperty(Environment.DATASOURCE, "java:marketChangersDS");
|
|
90
|
0
|
p.setProperty(Environment.DIALECT, HSQLDialect.class.getName());
|
|
91
|
0
|
p.setProperty(Environment.HBM2DDL_AUTO, "update");
|
|
92
|
|
|
|
93
|
0
|
if (values != null) {
|
|
94
|
0
|
try {
|
|
95
|
0
|
p.load(values);
|
|
96
|
|
} catch (IOException ioe) {
|
|
97
|
0
|
throw new RuntimeException(ioe);
|
|
98
|
|
}
|
|
99
|
|
}
|
|
100
|
|
|
|
101
|
0
|
Configuration config = new Configuration();
|
|
102
|
0
|
config.setProperties(p);
|
|
103
|
|
|
|
104
|
0
|
try {
|
|
105
|
0
|
config.addClass(PrayerRequest.class);
|
|
106
|
0
|
config.addClass(PrayerComment.class);
|
|
107
|
0
|
config.addClass(PrayerCategory.class);
|
|
108
|
0
|
config.addClass(UserContract.class);
|
|
109
|
|
} catch (MappingException me) {
|
|
110
|
0
|
throw new RuntimeException(me);
|
|
111
|
|
}
|
|
112
|
|
|
|
113
|
0
|
return config;
|
|
114
|
|
}
|
|
115
|
|
|
|
116
|
0
|
private void indexPrayerRequests(PicoContainer pico) {
|
|
117
|
0
|
PrayerRequestSearcher searcher =
|
|
118
|
|
(PrayerRequestSearcher) pico.getComponentInstanceOfType(
|
|
119
|
|
PrayerRequestSearcher.class);
|
|
120
|
|
|
|
121
|
0
|
SessionProvider sessionProvider =
|
|
122
|
|
(SessionProvider) pico.getComponentInstanceOfType(
|
|
123
|
|
SessionProvider.class);
|
|
124
|
|
|
|
125
|
0
|
try {
|
|
126
|
0
|
searcher.index(sessionProvider.getSession().find("from PrayerRequest"));
|
|
127
|
0
|
sessionProvider.close();
|
|
128
|
|
} catch (HibernateException he) {
|
|
129
|
0
|
throw new RuntimeException(he);
|
|
130
|
|
}
|
|
131
|
|
}
|
|
132
|
|
}
|